package com.example.secondhandshop.model;

import android.os.NetworkOnMainThreadException;
import android.util.Log;

import com.example.secondhandshop.bean.AllchatitemData;
import com.example.secondhandshop.bean.Chatitem;
import com.example.secondhandshop.bean.ResponseBody;
import com.example.secondhandshop.ui.activity.Constans;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Headers;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class ReadModel {
    OkHttpClient client = new OkHttpClient();
    public void Isread(int position,int messageid,ReadModel.OnProductsFetchedListener listener) {
        new Thread(() -> {
            // url路径
            String url = Constans.SERVER_URL()+"tran/chat/change?chatId="+messageid;
            // 请求头
            Headers headers = new Headers.Builder()
                    .add("Accept", "application/json, text/plain, */*")
                    .add("appId", Constans.getAppId())
                    .add("appSecret", Constans.getAppSecret())
                    .build();
            //请求组合创建
            Request request = new Request.Builder()
                    .url(url)
                    // 将请求头加至请求中
                    .headers(headers)
                    .get()
                    .build();
            try {
                OkHttpClient client = new OkHttpClient();
                //发起请求，传入callback进行回调
                client.newCall(request).enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                       if(response.code()==200){
                           listener.onSuccess(position);
                       }else {
                           listener.onFailure(0);
                       }
                    }
                });
            } catch (NetworkOnMainThreadException ex) {
                ex.printStackTrace();
            }
        }).start();
    }

    public interface OnProductsFetchedListener {
        void onSuccess(int i);
        void onFailure(int i);
    }
}